package arsw.twitter.view;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.Timer;

import arsw.twitter.common.Mensaje;
import arsw.twitter.common.IController;


public class VistaUsuario extends JFrame implements Actualizar {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DefaultListModel messagesListModel = new DefaultListModel();
	private DefaultListModel usersListModel = new DefaultListModel();
	private String usuario;
	
	private JTextField textMessage = new JTextField();
	private JTextField textFollow = new JTextField();
	
	private Timer polling;
	
	private IController uc;
	
	private VistaUsuario(String usuario, IController controller) {
		this.usuario = usuario;
		this.uc = controller;
		setTitle(usuario);
		setSize(640, 430);
		Show();
	}
	
	static public VistaUsuario newInstance(String usuario, IController controller) {
		return new VistaUsuario(usuario, controller);
	}
	
	public void Show()
	{
		JPanel userPane = new JPanel(new GridLayout(2,3));
		textMessage.setColumns(30);
		userPane.add(textMessage);

		JButton sendMessage = new JButton("Enviar");
		sendMessage.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				uc.publicar(usuario, textMessage.getText());
			}
		});
		userPane.add(sendMessage);
		userPane.add(new JLabel());
		
		textFollow.setColumns(30);
		userPane.add(textFollow);

		JButton addFollow = new JButton("Seguir");
		addFollow.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				uc.seguir(usuario, textFollow.getText());
			}
		});
		userPane.add(addFollow);

		JButton removeFollow = new JButton("Suspender");
		removeFollow.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
					uc.suspender(usuario, textFollow.getText());
			}
		});
		userPane.add(removeFollow);

		
		JPanel messagesPane = new JPanel(new GridLayout(1,2));
		JList jMessages = new JList(messagesListModel);
		JList jUsers    = new JList(usersListModel);
		
		messagesPane.add(jMessages);
		messagesPane.add(jUsers);

		
		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
		                           userPane, messagesPane);
		splitPane.setOneTouchExpandable(true);
		Dimension minimumSize = new Dimension(100, 50);
		messagesPane.setMinimumSize(minimumSize);

		add(splitPane);
		//Código encargado de revisar periodicamente
		this.polling = new Timer(5000, new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Iterable<Mensaje> mensajes = uc.leerMensajes(usuario);
				actualizarMensajes(mensajes);
			}
		});
		polling.start();
		
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				polling.stop();
				System.exit(0);
			}
		});

		setVisible(true);
	}

	public void clearMessages() {
		messagesListModel.clear();
	}

	public void clearUsers() {
		usersListModel.clear();
	}

	public void setMessage(String user, String message) {
		messagesListModel.addElement(user+": "+message);
	}

	public void setUser(String user) {
		messagesListModel.addElement(user);
	}
	
	@Override
	public void actualizarMensajes(Iterable<Mensaje> mensajes) {
		clearMessages();
		for(Mensaje m : mensajes) {
			setMessage(m.getUser(), m.getMessage());
		}
	}

	@Override
	public void actualizarUsuarios(Iterable<String> users) {
		clearUsers();
		for(String user : users) {
			setUser(user);
		}
	}
	    
}
